// pages/Intention_collection/Intention_collection.js
import {request} from "../../utils/request";
import config from "../../utils/config";

Page({

    /**
     * 页面的初始数据
     */
    data: {
        custom:wx.getMenuButtonBoundingClientRect(),
        phone:'',
        shop_name:'',
        shop_address:'',
        name:'',
        wechat_no:'',
        user_id:'',
        brand_id:'',
        good_id:'',
        discount_price:'',
        freight:'',
        formHeight:0,
    },
    onLoad(options) {
        var user = wx.getStorageSync('userInfo');
        var address = user.province + user.city + user.county
        if(address == 0){
            address = '';
        }
        this.setData({
            phone:user.phone,
            shop_name:user.shop_name,
            shop_address:address +user.shop_address,
            name:user.nickname,
            wechat_no:user.wechat_no,
            user_id:user.id,
            good_id:options.good_id
        })
        this.getGoodInfo()
        let query = wx.createSelectorQuery();
        query.select('.form').boundingClientRect(rect=>{
            this.setData({formHeight:rect.height})
        }).exec();
    },
    changeValue(e){
        var field = e.currentTarget.dataset.field,
            value = e.detail.value
        this.setData({[field]:value})
    },
    getGoodInfo(){
        request({
            url: "/miniprogram/system/getGoodDetail",
            data: {id: this.data.good_id},
            type: 'get'
        }).then(res => {
            if (res.code != 200) return false
            this.setData({
                brand_id:res.data.brand_id.id,
                discount_price:res.data.discount_price,
                freight:res.data.freight,
            })
        })
    },
    submit(){
        var {phone,shop_name,shop_address,name,wechat_no} = this.data
        if(!phone || !shop_name || !shop_address || !name || !wechat_no){
            config.toast('表中选项均为必传，请完整填写。',201,()=>{})
            return false
        }
        wx.showLoading()
        request({
            url:"/miniprogram/good/createGoodSole",
            data:{
                user_id:this.data.user_id,
                brand_id:this.data.brand_id,
                good_id:this.data.good_id,
                discount_price:this.data.discount_price,
                freight:this.data.freight,
                phone:this.data.phone,
                shop_name:this.data.shop_name,
                shop_address:this.data.shop_address,
                name:this.data.name,
                wechat_no:this.data.wechat_no,
            },
            type:'post'
        }).then(res=>{
            wx.hideLoading()
            config.toast(res.msg,201,()=>{
                setTimeout(()=>{
                    if(res.code == 200){
                        wx.navigateTo({url:"/pages/my_intention/my_intention"})
                    }else if(res.code == 202){
                        wx.navigateTo({url:"/pages/edit_address/edit_address"})
                    }
                },1500)
            })
        })
    }
})